/*pmFWCFu5nhKkBzYmrkBakXNT0YErLrPC4gji/9NriLKiYq+dqg5C7Ltooq33619h*/
import { base, ajax, cacheTools, cardCache, toast } from 'nc-lightapp-front';
import excelImportconfig from 'uap/common/components/excelImportconfig';
import tableButtonClick from './tableButtonClick';
import { tableId, formId, billType, dataSource } from '../constants';
import { buttonVisible, getButtonsKey, getInnerButtonkey, cardBodyAndInnerButtonVisible } from '../../../../public/components/pubUtils/buttonvisible.js';
let { setDefData, getDefData } = cardCache;
import { loginContext, getContext, loginContextKeys } from '../../../../public/components/arapInitInfo/loginContext';
import { OperationColumn } from '../../../../public/components/pubUtils/arapConstant';

export default function (props, callback) {
	const that = this;
	//从地址栏获取页面编码
	let appcode = props.getSearchParam('c');
	let pagecode = this.getPagecode();

	if (this.props.getUrlParam('srcbilltype')) {
		//处理外系统推单,改变appcode、pagecode
		dealOutToPaybill.call(this, props, appcode, pagecode)
	} else {
		if(this.props.getUrlParam('src') === '36S3'){
			pagecode = this.props.getUrlParam('billtype')
		}
		init(that, props, appcode, pagecode, callback);
	}
}

function init(that, props, appcode, pagecode, callback) {
	let excelimportconfig = excelImportconfig(props, "arap", billType, true, "", { "appcode": appcode, "pagecode": that.getExportPageCode() });
	let tradetype = pagecode
	props.createUIDom(
		{
			pagecode: pagecode,//页面id
			appcode: appcode,//注册按钮的id
			reqDataQueryallbtns: {
				rqUrl: '/arap/arappub/queryallbtns.do',
				rqJson: `{\n  \"pagecode\": \"${pagecode}\",\n  \"appcode\": \"${appcode}\"\n,\n  \"billtype\": \"${billType}\"\n,\n  \"tradetype\": \"${tradetype}\"\n}`,
				rqCode: 'button'
			},
			reqDataQuerypage: {
				rqUrl: '/arap/arappub/querypage.do',
				rqJson: `{\n  \"pagecode\": \"${pagecode}\",\n  \"appcode\": \"${appcode}\"\n}`,
				rqCode: 'template'
			}
		},
		function (data) {
			if (data) {
				if (!data.template[tableId]) {
					return;
				}
				if (data.template) {
					let meta = data.template;
					meta = modifierMeta(that, props, meta);
					props.meta.setMeta(meta);
				}
				if (data.button && data.button.button) {
					let button = data.button.button;
					let pullBillInfoVOAry = data.button.pullbillinfo;
					getButtonsKey(button, that.Info.allButtonsKey);//保存所有头部和肩部按钮
					that.Info.pullBillInfoVOAry = pullBillInfoVOAry;
					props.button.setButtons(button);
					props.button.setUploadConfig("ImportData", excelimportconfig);
				}
				if (callback) {
					callback();
				}
				if (data.context) {
					// 初始化上下文变量
					loginContext(data.context);
					if (getContext(loginContextKeys.transtype)) {
						if (that.refs.tradetypeBtn) {
							that.refs.tradetypeBtn.setVisible(false);
						}
					}
				}

			}
		}
		//false//请求模板不走缓存
	)
}



function modifierMeta(that, props, meta) {
	let status = props.getUrlParam('status');
	meta[formId].status = status;
	meta[tableId].status = status;
	//添加操作列
	meta[tableId].items.push({
		label: that.state.json['paybill-000007'],/* 国际化处理： 操作*/
		itemtype: 'customer',
		attrcode: 'opr',
		width: OperationColumn,
		visible: true,
		fixed: 'right',
		render: (text, record, index) => {
			let trueBtn = cardBodyAndInnerButtonVisible(that, that.state.buttonfalg, record.expandRowStatus);
			return props.button.createOprationButton(trueBtn, {
				area: "card_inner",
				buttonLimit: 3,
				onButtonClick: (props, key) => tableButtonClick(that, props, key, text, record, index)
			});
		}
	});

	return meta;
}



//处理外系统推付款单
export function dealOutToPaybill(props, appcode, pagecode) {
	let url = "";
	let data = {};
	let srcbilltype = this.props.getUrlParam('srcbilltype')
	if (srcbilltype == '21') {//采购订单订单付款
		url = "/nccloud/arap/paybill/putopaybill.do",
			data = {
				pk_bills: cacheTools.get('21ToF3Pks'),
				pageId: pagecode
			}
	} else if (srcbilltype == 'FCT1') {//付款合同付款
		url = "/nccloud/arap/paybill/fct1topaybill.do",
			data = {
				pk_bills: cacheTools.get('fct1ToF3Pks'),
				pageId: pagecode
			}
	} else if (srcbilltype == 'Z2') {//采购合同付款
		url = "/nccloud/arap/paybill/z2topaybill.do",
			data = {
				pk_bills: cacheTools.get('Z2TOD3Pks'),
				pageId: pagecode
			}
	}
	ajax({
		url: url,
		data: data,
		success: (res) => {
			if (res.data) {
				pagecode = res.data.head[this.formId].rows[0].values.pk_tradetype.value;
				this.data = res.data
				init(this, props, appcode, pagecode, this.initShow)
			} else {
				this.props.form.EmptyAllFormValue(this.formId);
				this.props.cardTable.setTableData(this.tableId, { rows: [] });
			}
		}
	})
}

/*pmFWCFu5nhKkBzYmrkBakXNT0YErLrPC4gji/9NriLKiYq+dqg5C7Ltooq33619h*/